﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletController : MonoBehaviour
{
    public BulletState bulletState;
    public float awakeTime = 0;

    private float flyTime = 0;
    public Transform shooter;

    public void SetShooter(Transform s)
    {
        shooter = s;
    }
    // Start is called before the first frame update
    void Start()
    {
        flyTime = GameInfo.Instance.GetBulletAwakeTime();


    }
    private void OnEnable()
    {
        awakeTime = Time.time;
        bulletState = BulletState.FLAYING;
        
     
    }
    // Update is called once per frame
    void Update()
    {
        
    }
    public void Aimed()
    {
        bulletState = BulletState.IDLE;
        BulletSleep();
    }
    private void LateUpdate()
    {
        if (bulletState == BulletState.FLAYING)
        {
            if (Time.time - awakeTime >= flyTime)
            {
                //Debug.Log("Sleep");
                bulletState = BulletState.IDLE;
                BulletSleep();
            }
        }
    }
    void BulletSleep()
    {
        bulletState = BulletState.IDLE;
        if(tag==GameInfo.Instance.bullet_tag_android)
        {
            GameInfo.Instance.AndroidBulletPool.Enqueue(transform);
        }
        if(tag ==GameInfo.Instance.bullet_tag_player)
        {
            GameInfo.Instance.PlayerBulletPool.Enqueue(transform);
        }        
        transform.localPosition = Vector3.zero;
        gameObject.GetComponent<Rigidbody>().Sleep();
        gameObject.SetActive(false);
    }
    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log(collision.gameObject.tag);
        if(collision.gameObject.tag!="player"|| collision.gameObject.tag!="android")
        {
            Aimed();
        }
    }

}
